package com.xtkj.superclub_shop.interceptor;

import com.xtkj.commons.TokenUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lizheqi
 * @create 2019-12-23-18:32
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("utf-8");
        String token = request.getHeader("token");
        System.out.println(token+"=========");
        if (token != null){
            boolean result = TokenUtil.verify(token);
            System.out.println(result);
            if(result){
                return true;
            }
            System.out.println("非法访问");
            response.getWriter().write("306");
        }
        System.out.println("认证失败");
        response.getWriter().write("50000");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
